import { _decorator, Button, Component, EditBox, Event, Label, Node } from 'cc';
import { WebSocketClient } from './WebSocketClient';
const { ccclass, property } = _decorator;

@ccclass('WebSocketGameManager')
export class WebSocketGameManager extends Component {
    @property(Label)
    MessageLabel: Label = null;
    @property(EditBox)
    MessageEditBox: EditBox = null;
    @property(Button)
    SendButton: Button = null;

    public static MessageEventHandler: Node = new Node();

    protected onEnable(): void {
        this.SendButton.node.on(Button.EventType.CLICK, this.send, this);
        WebSocketGameManager.MessageEventHandler.on("MessageUpdate", this.eventHandler, this);
    }
    protected onDisable(): void {
        this.SendButton.node.off(Button.EventType.CLICK, this.send, this);
        WebSocketGameManager.MessageEventHandler.off("MessageUpdate", this.eventHandler, this);
    }


    send() {
        if (this.MessageEditBox.string != "") {
            WebSocketClient.send(this.MessageEditBox.string);
            this.MessageEditBox.string = "";
        }
    }

    eventHandler(any) {
        this.MessageLabel.string = String(any);
    }

}


